package com.zjb.demo.system.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.mybatisflex.core.paginate.Page;
import com.zjb.demo.common.domain.dto.SysLoginInfo;
import com.zjb.demo.common.domain.response.AjaxResult;
import com.zjb.demo.system.domain.SysPost;
import com.zjb.demo.system.domain.query.SysPostQuery;
import com.zjb.demo.system.service.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * @author 庄佳彬
 * @since 2023/12/30 22:50
 */
@Controller
@Mapping("/system/post")
@Api(tags = "系统：岗位管理")
public class SysPostController {
    @Inject
    private SysPostService postService;

    @Get
    @Mapping("/list")
    @ApiOperation(value = "岗位列表")
    public AjaxResult<Page<SysPost>> list(SysPostQuery query) {
        return AjaxResult.success(postService.pageList(query));
    }

    @Get
    @Mapping("/export")
    @ApiOperation(value = "导出岗位列表")
    public void export(SysPostQuery query) throws IOException {
        var list = postService.list(query);
        var ctx = Context.current();
        ctx.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        ctx.headerAdd("Content-Disposition", "attachment;filename=" + URLEncoder.encode("岗位列表", StandardCharsets.UTF_8) + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx");
        var os = ctx.outputStream();
        EasyExcel.write(os, SysLoginInfo.class)
                .sheet("岗位列表")
                .doWrite(list);
    }

    @Get
    @Mapping("/{postId}")
    @ApiOperation(value = "岗位详细")
    public AjaxResult<SysPost> get(Long postId) {
        return AjaxResult.success(postService.getById(postId));
    }

    @Post
    @Mapping
    @ApiOperation(value = "岗位新增")
    public AjaxResult<Boolean> save(@Body SysPost sysPost) {
        if (postService.existsCode(sysPost.getPostCode())) {
            return AjaxResult.fail("岗位编码已存在");
        }
        if (postService.existsName(sysPost.getPostName())) {
            return AjaxResult.fail("岗位名称已存在");
        }
        return AjaxResult.success(postService.save(sysPost));
    }

    @Put
    @Mapping
    @ApiOperation(value = "岗位修改")
    public AjaxResult<Boolean> update(@Body SysPost sysPost) {
        if (postService.existsCode(sysPost.getPostCode())) {
            return AjaxResult.fail("岗位编码已存在");
        }
        if (postService.existsName(sysPost.getPostName())) {
            return AjaxResult.fail("岗位名称已存在");
        }
        return AjaxResult.success(postService.updateById(sysPost));
    }

    @Delete
    @Mapping("/{postIds}")
    @ApiOperation(value = "岗位删除")
    public AjaxResult<Boolean> delete(Long[] postIds) {
        return AjaxResult.success(postService.removeByIds(List.of(postIds)));
    }

    @Get
    @Mapping("/optionselect")
    @ApiOperation(value = "获取岗位选择框列表")
    public AjaxResult<List<SysPost>> optionselect() {
        return AjaxResult.success(postService.list());
    }
}
